<template>
  <div class="min-h-screen bg-base-200 flex">
    <!-- 左侧边栏 -->
    <div id="sidebar" class="sidebar sidebar-expanded bg-base-100 shadow-xl border-r border-base-300 flex flex-col transition-all duration-400">
      <!-- 应用标题 -->
      <div class="p-6 border-b border-base-300">
        <div class="flex items-center justify-between">
          <div class="flex items-center space-x-3 fade-text">
            <div class="w-10 h-10 rounded-xl bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center shadow-lg">
              <i class="fas fa-brain text-white text-lg"></i>
            </div>
            <div>
              <h1 class="font-bold text-xl bg-gradient-to-r from-indigo-600 to-purple-600 bg-clip-text text-transparent">博沃智慧</h1>
              <p class="text-xs text-base-content/70">系统设置</p>
            </div>
          </div>
          <button id="sidebarToggle" class="btn btn-ghost btn-sm">
            <i class="fas fa-bars text-base-content"></i>
            <i class="fas fa-chevron-left text-base-content hidden"></i>
          </button>
        </div>
      </div>

      <!-- 导航菜单 -->
      <div class="flex-1 overflow-y-auto custom-scrollbar p-4">
        <nav class="space-y-2">
          <router-link to="/chat" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-base-200 transition-colors fade-text">
            <i class="fas fa-comments text-base-content/70"></i>
            <span class="text-base-content">对话界面</span>
          </router-link>
          <router-link to="/settings" class="flex items-center space-x-3 p-3 rounded-lg bg-primary/10 text-primary border border-primary/20 fade-text">
            <i class="fas fa-cog text-primary"></i>
            <span class="font-medium">系统设置</span>
          </router-link>
          <router-link to="/knowledge" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-base-200 transition-colors fade-text">
            <i class="fas fa-book text-base-content/70"></i>
            <span class="text-base-content">知识库管理</span>
          </router-link>
          <router-link to="/mcp-management" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-base-200 transition-colors fade-text">
            <i class="fas fa-plug text-base-content/70"></i>
            <span class="text-base-content">MCP管理</span>
          </router-link>
        </nav>
      </div>

      <!-- 用户信息 -->
      <div class="p-4 border-t border-base-300">
        <div class="flex items-center space-x-3">
          <div class="avatar">
            <div class="w-12 h-12 rounded-full bg-gradient-to-br from-emerald-400 to-cyan-500 flex items-center justify-center shadow-lg">
              <span class="text-white text-lg font-bold">{{ authStore.username?.charAt(0).toUpperCase() }}</span>
            </div>
          </div>
          <div class="fade-text flex-1">
            <p class="font-semibold text-base-content">{{ authStore.username }}</p>
            <p class="text-xs text-base-content/70">专业版用户</p>
          </div>
          <div class="fade-text">
            <router-link to="/chat" class="btn btn-ghost btn-sm">
              <i class="fas fa-sign-out-alt text-base-content/70"></i>
            </router-link>
          </div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="flex-1 flex flex-col bg-base-100">
      <!-- 标题栏 -->
      <div class="bg-base-100 border-b border-base-300 p-6">
        <div class="flex items-center justify-between">
          <div>
            <h1 class="text-2xl font-bold text-base-content">系统设置</h1>
            <p class="text-base-content/70 mt-1">管理您的账户、偏好设置和个人知识库</p>
          </div>
          <div class="flex items-center space-x-4">
            <button class="btn btn-ghost btn-sm">
              <i class="fas fa-question-circle mr-2"></i>帮助
            </button>
          </div>
        </div>
      </div>

      <!-- 标签页导航 -->
      <div class="bg-base-100 border-b border-base-300 px-6">
        <div class="tabs tabs-lifted">
          <button 
            v-for="tab in tabs" 
            :key="tab.id"
            @click="activeTab = tab.id"
            :class="[
              'tab tab-lifted',
              activeTab === tab.id ? 'tab-active text-primary' : 'text-base-content/70'
            ]"
          >
            <i :class="['mr-2', tab.icon]"></i>
            {{ tab.name }}
          </button>
        </div>
      </div>

      <!-- 设置内容 -->
      <div class="flex-1 overflow-y-auto custom-scrollbar p-6">
        <!-- 账户设置 -->
        <div v-if="activeTab === 'account'" class="max-w-4xl mx-auto space-y-6">
          <!-- 个人信息 -->
          <div class="card bg-base-100 shadow-sm border border-base-300">
            <div class="card-body">
              <h2 class="card-title">个人信息</h2>
              <div class="flex items-start space-x-6">
                <div class="avatar">
                  <div class="w-24 h-24 rounded-full bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center shadow-lg cursor-pointer hover:scale-105 transition-transform">
                    <span class="text-white text-2xl font-bold">{{ authStore.username?.charAt(0).toUpperCase() }}</span>
                  </div>
                </div>
                <div class="flex-1 space-y-4">
                  <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div class="form-control">
                      <label class="label">
                        <span class="label-text">用户名</span>
                      </label>
                      <input 
                        type="text" 
                        :value="authStore.username"
                        readonly
                        class="input input-bordered bg-base-200"
                      />
                    </div>
                    <div class="form-control">
                      <label class="label">
                        <span class="label-text">邮箱</span>
                      </label>
                      <input 
                        type="email" 
                        :value="authStore.user?.email"
                        readonly
                        class="input input-bordered bg-base-200"
                      />
                    </div>
                  </div>
                  <div class="form-control">
                    <label class="label">
                      <span class="label-text">个人简介</span>
                    </label>
                    <textarea 
                      rows="3" 
                      class="textarea textarea-bordered resize-none"
                      placeholder="介绍一下您自己..."
                    >数据分析师，专注于环境数据分析和可视化</textarea>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 密码修改 -->
          <div class="card bg-base-100 shadow-sm border border-base-300">
            <div class="card-body">
              <h2 class="card-title">修改密码</h2>
              <div class="space-y-4">
                <div class="form-control">
                  <label class="label">
                    <span class="label-text">当前密码</span>
                  </label>
                  <input 
                    type="password" 
                    class="input input-bordered"
                    placeholder="请输入当前密码"
                  />
                </div>
                <div class="form-control">
                  <label class="label">
                    <span class="label-text">新密码</span>
                  </label>
                  <input 
                    type="password" 
                    class="input input-bordered"
                    placeholder="请输入新密码"
                  />
                </div>
                <div class="form-control">
                  <label class="label">
                    <span class="label-text">确认新密码</span>
                  </label>
                  <input 
                    type="password" 
                    class="input input-bordered"
                    placeholder="请再次输入新密码"
                  />
                </div>
                <div class="flex space-x-4">
                  <button class="btn btn-primary">保存密码</button>
                  <button class="btn btn-ghost">取消</button>
                </div>
              </div>
            </div>
          </div>

          <!-- 安全设置 -->
          <div class="card bg-base-100 shadow-sm border border-base-300">
            <div class="card-body">
              <h2 class="card-title">安全设置</h2>
              <div class="space-y-4">
                <div class="flex items-center justify-between">
                  <div>
                    <p class="font-medium text-base-content">两步验证</p>
                    <p class="text-sm text-base-content/70">为您的账户添加额外的安全保护</p>
                  </div>
                  <input type="checkbox" class="toggle toggle-primary" />
                </div>
                <div class="flex items-center justify-between">
                  <div>
                    <p class="font-medium text-base-content">会话超时</p>
                    <p class="text-sm text-base-content/70">30分钟无操作后自动退出登录</p>
                  </div>
                  <input type="checkbox" class="toggle toggle-primary" checked />
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 偏好设置 -->
        <div v-if="activeTab === 'preferences'" class="max-w-4xl mx-auto space-y-6">
          <!-- 界面设置 -->
          <div class="card bg-base-100 shadow-sm border border-base-300">
            <div class="card-body">
              <h2 class="card-title">界面设置</h2>
              <div class="space-y-4">
                <div class="flex items-center justify-between">
                  <div>
                    <p class="font-medium text-base-content">深色模式</p>
                    <p class="text-sm text-base-content/70">减少眼睛疲劳，适合夜间使用</p>
                  </div>
                  <input type="checkbox" class="toggle toggle-primary" />
                </div>
                <div class="flex items-center justify-between">
                  <div>
                    <p class="font-medium text-base-content">紧凑布局</p>
                    <p class="text-sm text-base-content/70">显示更多内容，减少空白空间</p>
                  </div>
                  <input type="checkbox" class="toggle toggle-primary" />
                </div>
                <div class="flex items-center justify-between">
                  <div>
                    <p class="font-medium text-base-content">消息提醒</p>
                    <p class="text-sm text-base-content/70">新消息时显示桌面通知</p>
                  </div>
                  <input type="checkbox" class="toggle toggle-primary" checked />
                </div>
              </div>
            </div>
          </div>

          <!-- 语言设置 -->
          <div class="card bg-base-100 shadow-sm border border-base-300">
            <div class="card-body">
              <h2 class="card-title">语言设置</h2>
              <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div class="form-control">
                  <label class="label">
                    <span class="label-text">界面语言</span>
                  </label>
                  <select class="select select-bordered">
                    <option value="zh-CN" selected>简体中文</option>
                    <option value="zh-TW">繁体中文</option>
                    <option value="en-US">English</option>
                  </select>
                </div>
                <div class="form-control">
                  <label class="label">
                    <span class="label-text">模型语言</span>
                  </label>
                  <select class="select select-bordered">
                    <option value="zh-CN" selected>中文</option>
                    <option value="en-US">English</option>
                    <option value="auto">自动检测</option>
                  </select>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 使用统计 -->
        <div v-if="activeTab === 'statistics'" class="max-w-6xl mx-auto space-y-6">
          <!-- 使用概览 -->
          <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
            <div class="stat bg-gradient-to-br from-blue-500 to-blue-600 text-white">
              <div class="stat-figure">
                <i class="fas fa-comments text-3xl"></i>
              </div>
              <div class="stat-title text-blue-100">总对话数</div>
              <div class="stat-value text-2xl">1,234</div>
              <div class="stat-desc text-blue-100">↗︎ 12.5%</div>
            </div>
            
            <div class="stat bg-gradient-to-br from-green-500 to-green-600 text-white">
              <div class="stat-figure">
                <i class="fas fa-calendar-check text-3xl"></i>
              </div>
              <div class="stat-title text-green-100">本月活跃</div>
              <div class="stat-value text-2xl">89</div>
              <div class="stat-desc text-green-100">↗︎ 8.3%</div>
            </div>
            
            <div class="stat bg-gradient-to-br from-yellow-500 to-yellow-600 text-white">
              <div class="stat-figure">
                <i class="fas fa-coins text-3xl"></i>
              </div>
              <div class="stat-title text-yellow-100">Token使用</div>
              <div class="stat-value text-2xl">45.6K</div>
              <div class="stat-desc text-yellow-100">↗︎ 23.1%</div>
            </div>
            
            <div class="stat bg-gradient-to-br from-purple-500 to-purple-600 text-white">
              <div class="stat-figure">
                <i class="fas fa-thumbs-up text-3xl"></i>
              </div>
              <div class="stat-title text-purple-100">满意度</div>
              <div class="stat-value text-2xl">94.2%</div>
              <div class="stat-desc text-purple-100">↗︎ 2.1%</div>
            </div>
          </div>

          <!-- 功能使用分布 -->
          <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div class="card bg-base-100 shadow-sm border border-base-300">
              <div class="card-body">
                <h2 class="card-title">功能使用分布</h2>
                <div class="space-y-3">
                  <div class="flex justify-between items-center">
                    <span class="text-base-content">数据问答</span>
                    <div class="flex items-center space-x-2">
                      <div class="w-24 bg-base-200 rounded-full h-2">
                        <div class="bg-primary h-2 rounded-full" style="width: 35%"></div>
                      </div>
                      <span class="text-sm text-base-content/70">35%</span>
                    </div>
                  </div>
                  <div class="flex justify-between items-center">
                    <span class="text-base-content">知识问答</span>
                    <div class="flex items-center space-x-2">
                      <div class="w-24 bg-base-200 rounded-full h-2">
                        <div class="bg-secondary h-2 rounded-full" style="width: 25%"></div>
                      </div>
                      <span class="text-sm text-base-content/70">25%</span>
                    </div>
                  </div>
                  <div class="flex justify-between items-center">
                    <span class="text-base-content">代码审查</span>
                    <div class="flex items-center space-x-2">
                      <div class="w-24 bg-base-200 rounded-full h-2">
                        <div class="bg-accent h-2 rounded-full" style="width: 20%"></div>
                      </div>
                      <span class="text-sm text-base-content/70">20%</span>
                    </div>
                  </div>
                  <div class="flex justify-between items-center">
                    <span class="text-base-content">图片分析</span>
                    <div class="flex items-center space-x-2">
                      <div class="w-24 bg-base-200 rounded-full h-2">
                        <div class="bg-neutral h-2 rounded-full" style="width: 15%"></div>
                      </div>
                      <span class="text-sm text-base-content/70">15%</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="card bg-base-100 shadow-sm border border-base-300">
              <div class="card-body">
                <h2 class="card-title">响应时间统计</h2>
                <div class="space-y-3">
                  <div class="flex justify-between items-center">
                    <span class="text-base-content">平均响应时间</span>
                    <span class="font-medium">1.2秒</span>
                  </div>
                  <div class="w-full bg-base-200 rounded-full h-2">
                    <div class="bg-green-500 h-2 rounded-full" style="width: 85%"></div>
                  </div>
                  <div class="flex justify-between items-center">
                    <span class="text-base-content">最快响应</span>
                    <span class="font-medium">0.3秒</span>
                  </div>
                  <div class="flex justify-between items-center">
                    <span class="text-base-content">最慢响应</span>
                    <span class="font-medium">3.8秒</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useAuthStore } from '../stores/auth';

const authStore = useAuthStore();

const activeTab = ref('account');
const sidebarCollapsed = ref(false);

const tabs = [
  {
    id: 'account',
    name: '账户设置',
    icon: 'fas fa-user'
  },
  {
    id: 'preferences',
    name: '偏好设置',
    icon: 'fas fa-sliders-h'
  },
  {
    id: 'statistics',
    name: '使用统计',
    icon: 'fas fa-chart-bar'
  }
];

// 侧边栏折叠功能
const toggleSidebar = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value;
  const sidebar = document.getElementById('sidebar');
  const fadeTexts = document.querySelectorAll('.fade-text');
  const barsIcon = document.querySelector('#sidebarToggle .fa-bars');
  const chevronIcon = document.querySelector('#sidebarToggle .fa-chevron-left');
  
  if (sidebarCollapsed.value) {
    sidebar?.classList.add('sidebar-collapsed');
    sidebar?.classList.remove('sidebar-expanded');
    barsIcon?.classList.add('hidden');
    chevronIcon?.classList.remove('hidden');
  } else {
    sidebar?.classList.remove('sidebar-collapsed');
    sidebar?.classList.add('sidebar-expanded');
    barsIcon?.classList.remove('hidden');
    chevronIcon?.classList.add('hidden');
  }
  
  setTimeout(() => {
    fadeTexts.forEach((text: any) => {
      if (sidebarCollapsed.value) {
        text.classList.add('hidden');
      } else {
        text.classList.remove('hidden');
      }
    });
  }, sidebarCollapsed.value ? 0 : 200);
};

onMounted(() => {
  const sidebarToggle = document.getElementById('sidebarToggle');
  if (sidebarToggle) {
    sidebarToggle.addEventListener('click', toggleSidebar);
  }
});
</script>

<style scoped>
/* 自定义滚动条 */
.custom-scrollbar::-webkit-scrollbar {
  width: 6px;
}

.custom-scrollbar::-webkit-scrollbar-track {
  background: hsl(var(--b2));
  border-radius: 3px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
  background: hsl(var(--bc) / 0.3);
  border-radius: 3px;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
  background: hsl(var(--bc) / 0.5);
}

/* 侧边栏动画 */
.sidebar {
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-collapsed {
  width: 64px !important;
}

.sidebar-expanded {
  width: 15% !important;
}

/* 文本淡入淡出 */
.fade-text {
  transition: opacity 0.3s ease-in-out;
}

.fade-text.hidden {
  opacity: 0;
  pointer-events: none;
}
</style>